package com.sharemarking.wa.platform.mapper;

import com.sharemarking.wa.platform.dto.company.CarListDto;
import com.sharemarking.wa.platform.dto.company.CompanyDto;
import com.sharemarking.wa.platform.dto.company.CompanyInfoDto;
import com.sharemarking.wa.platform.dto.company.DriverListDto;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author dxy
 */
@Repository
public interface CompanyMapper {
    @Select("SELECT IFNULL(a.real_name,a.name) AS companyName,b.mobile FROM sys_org  a LEFT JOIN sys_member b ON a.member_id = b.id " +
            "WHERE a.id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId})")
    CompanyInfoDto getCompanyInfo(Integer memberId);

    @Select("SELECT IFNULL(a.real_name,a.name) AS companyName,b.mobile,c.address AS companyAddress," +
            "(SELECT attachment FROM wa_org_verify_attachment WHERE org_verify_id = c.id AND type = 1) AS licenseUrl, " +
            "(SELECT attachment FROM wa_org_verify_attachment WHERE org_verify_id = c.id AND type = 2) AS dangerousUrl " +
            "FROM sys_org a LEFT JOIN sys_member b ON a.member_id = b.id LEFT JOIN wa_org_verify c ON a.member_id = c.member_id\n" +
            "WHERE a.id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId} LIMIT 1)")
    CompanyDto getCompany(Integer memberId);

    @Select("SELECT IFNULL(a.real_name,a.name) AS companyName,b.mobile,c.address AS companyAddress," +
            "(SELECT attachment FROM wa_org_verify_attachment WHERE org_verify_id = c.id AND type = 1) AS licenseUrl, " +
            "(SELECT attachment FROM wa_org_verify_attachment WHERE org_verify_id = c.id AND type = 2) AS dangerousUrl " +
            " FROM sys_org a LEFT JOIN sys_member b ON a.member_id = b.id LEFT JOIN wa_org_verify c ON a.member_id = c.member_id\n" +
            "WHERE a.id = (SELECT org_id FROM wa_driver_verify WHERE member_id = #{memberId} LIMIT 1)")
    CompanyDto getDriverCompany(Integer memberId);

    @Select("SELECT number,type,length FROM wa_org_car WHERE org_id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId}) LIMIT #{pageIndex},#{pageSize}")
    List<CarListDto> getCarList(Map map);

    @Select("SELECT a.name AS name,c.mobile FROM wa_driver_verify a\n" +
            "LEFT JOIN sys_member c ON c.id = a.member_id \n" +
            "WHERE a.exam_state = 1 AND a.org_id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId}) LIMIT #{pageIndex},#{pageSize}")
    List<DriverListDto> getDriverList(Map map);

    @Select("SELECT COUNT(*) FROM wa_org_car WHERE org_id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId})")
    Integer getCarListCount(Map map);

    @Select("SELECT COUNT(*) FROM wa_driver_verify a\n" +
            "LEFT JOIN sys_member c ON c.id = a.member_id \n" +
            "WHERE a.exam_state = 1 AND a.org_id = (SELECT org_id FROM sys_org_member WHERE member_id = #{memberId}) LIMIT #{pageIndex},#{pageSize}")
    Integer getDriverListCount(Map map);
}
